package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.Device;
import org.dromara.system.domain.GatewayReport;
import org.dromara.system.domain.bo.DeviceBo;
import org.dromara.system.domain.bo.GatewayReportBo;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.vo.DeviceVo;
import org.dromara.system.domain.vo.GatewayReportVo;
import org.dromara.system.mapper.GatewayReportMapper;
import org.dromara.system.service.IGatewayReportService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户 业务层
 *
 * @author Lion Li
 */

@RequiredArgsConstructor
@Service
public class GatewayReportServiceImpl implements IGatewayReportService {
    private final GatewayReportMapper gatewayReportMapper;

    DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    @Override
    public TableDataInfo<GatewayReportVo> selectPageList(GatewayReportBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GatewayReport> lqw = buildQueryWrapper(bo);
        Page<GatewayReportVo> result = gatewayReportMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<GatewayReport> buildQueryWrapper(GatewayReportBo bo) {
        LambdaQueryWrapper<GatewayReport> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), GatewayReport::getName, bo.getName());
        if (bo.getStartTime() != null && !bo.getStartTime().isBlank() && bo.getEndTime() != null && !bo.getEndTime().isBlank()) {
            LocalDate startDate = LocalDate.parse(bo.getStartTime(), DATE_TIME_FORMATTER);
            LocalDateTime startTime1 = startDate.atStartOfDay();
            LocalDate endDate = LocalDate.parse(bo.getEndTime(), DATE_TIME_FORMATTER);
            LocalDateTime endTime1 = endDate.atTime(23, 59, 59);
            // 将 startTime1 和 endTime1 转换为 YYYY-MM-DD HH:MM:SS 格式的字符串
            DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String startTimeStr = startTime1.format(outputFormatter);
            String endTimeStr = endTime1.format(outputFormatter);
            lqw.between(GatewayReport::getCreateTime, startTimeStr, endTimeStr);
        }
        lqw.orderByDesc(GatewayReport::getCreateTime);
        return lqw;
    }

    @Override
    public void insertReport(GatewayReport gatewayReport) {
        gatewayReportMapper.insert(gatewayReport);
    }
}
